<?php


namespace App\Admin\Actions\Review;


use App\Admin\Actions\RowAction;
use App\Transverse;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class TransverseReview extends RowAction
{
    public $name = '审批';

    public function handle(Transverse $model, Request $request)
    {
        $user = Admin::user();
        $status = $request->get('status') ?: 0;
        $remark = $request->get('remark');
        $file = $request->file('file');
        try {
            if ($file) {
                $model->storeFile($file, 'file');
            }
            $model->review_status = $status;
            $model->review_remark = $remark;
            $model->save();
            $model->writeLog($user, data_get([1 => '【审批】通过', 2 => '【审批】不通过', 3 => '【审批】退回修改'], $status), $remark);
        } catch (\Exception $exception) {
            return $this->response()->error('审核失败！')->refresh();
        }
        return $this->response()->success('审核成功！')->location('');
    }

    public function form()
    {
        $this->radio('status', '审核状态')->options([1 => '通过', 2 => '不通过', 3 => '退回修改'])->required();
        $this->textarea('remark', '备注');
        $this->file('file', '合同附件')->help('非必选');
    }

}
